<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>vue-router导航守卫</title>
    <script src="../../vue.js" type="text/javascript"></script>
    <script src="vue-router.js" type="text/javascript"></script>
</head>
<body>
<div id="app">
    <router-link to="/">index</router-link>
    <router-link to="/user">user</router-link>
    <router-link to="/login">login</router-link>
    <router-view></router-view>
</div>
<script>
    var router=new VueRouter({
        routes:[
            {
                path:'/',
                component:{
                    template:`<div>首页</div>`
                }
            },
            {
                path:'/user',
                component:{
                    template:`<div>user</div>`
                },
                beforeEnter:(to,from,next)=>{
                    var login=true;
                    if(!login){
                        next('/login')
                    }else{
                        next()
                    }
                }
            },
            {
                path:'/login',
                component:{
                    template:`<div>login</div>`
                }
            }
        ]
    })
    // router.beforeEach(function (to,from,next) {
    //     var login=false;
    //     if(!login && to.path==="/user"){
    //         next('/login')
    //     }else{
    //         next()
    //     }
    // });

    var app=new Vue({
        el: "#app",
        router:router
    })
</script>
</body>
</html>